package com.kanshu.ksgb.fastread.doudou.common.business.ad.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.toutiao.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Title:
 * Description: 阅读器广告视频弹框
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/10/19 14:41
 *
 * @author EvanHe
 * @version 3.0
 */
public class ReaderAdDialog extends Dialog {
    public static final String PLAY_AD = "1";
    @BindView(R.id.read_overtime_tips)
    TextView mReadOvertimeTips;
    @BindView(R.id.continue_read)
    TextView mContinueRead;
    @BindView(R.id.vertical_divider)
    View mVerticalDivider;
    @BindView(R.id.okay)
    TextView mOkay;

    private ReaderAdListener mListener;

    private ReaderAdDialog(@NonNull Context context, String minute, String adType) {
        super(context, R.style.dialog_style);
        setContentView(R.layout.dialog_reader_video_ad);
        ButterKnife.bind(this);
        mReadOvertimeTips.setText(StringUtils.getString(R.string.read_overtime_tips, minute));
        if (TextUtils.equals(adType, PLAY_AD)) {
            mContinueRead.setVisibility(View.GONE);
            mVerticalDivider.setVisibility(View.GONE);
        } else {
            mContinueRead.setVisibility(View.VISIBLE);
            mVerticalDivider.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.okay, R.id.continue_read})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.okay:
                if (mListener != null) {
                    mListener.playAdVideo(mOkay);
                }
                break;
            case R.id.continue_read:
                if (mListener != null) {
                    mListener.continueRead();
                }
                dismiss();
                break;
        }
    }

    /**
     * @param minute 阅读超时
     * @param adType 是否强制播放广告
     */
    public static ReaderAdDialog show(Activity activity, String minute, String adType) {
        if (activity == null || activity.isFinishing()) {
            return null;
        }
        ReaderAdDialog dlg = new ReaderAdDialog(activity, minute, adType);
        dlg.setCanceledOnTouchOutside(false);
        dlg.setCancelable(false);
        dlg.show();
        return dlg;
    }

    public void setReadAdListener(ReaderAdListener readerAdListener) {
        mListener = readerAdListener;
    }

    public interface ReaderAdListener {
        void playAdVideo(TextView view);

        void continueRead();
    }

}
